﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BL;
using TestGuiApplication.Properties;



namespace TestGuiApplication
{
    public partial class TestGui : Form
    {
        cGame game;

        public TestGui()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;
           
        }

        void game_LocationUpdate(int playerID)
        {
            //this.lstEvents.Items.Add("Team: " + team.TeamColor + ", PlayerID: " + playerID + " Loacation: " + player.GetCurrLocation().ToString());
            this.lstEvents.Items.Add(playerID);
            updateGui();
        }


        void updateGui()
        {
            Pen p = new Pen(game.MyTeam.TeamColor);
            pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.MyTeam.PlayersList[0].GetCurrLocation(), new Size(50, 50)));
            pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.MyTeam.PlayersList[1].GetCurrLocation(), new Size(50, 50)));
            pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.MyTeam.PlayersList[2].GetCurrLocation(), new Size(50, 50)));

            if (game.EnemyTeam != null)
            {
                p = new Pen(game.EnemyTeam.TeamColor);
                pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.EnemyTeam.PlayersList[0].GetCurrLocation(), new Size(50, 50)));
                pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.EnemyTeam.PlayersList[1].GetCurrLocation(), new Size(50, 50)));
                pnlMap.CreateGraphics().DrawEllipse(p, new Rectangle(game.EnemyTeam.PlayersList[2].GetCurrLocation(), new Size(50, 50)));                
            }


        }

        private void tmrUpdate_Tick(object sender, EventArgs e)
        {
            updateGui();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            game = new cGame();
            game.LocationUpdate += new cGame.PlayerUpdateLisner(game_LocationUpdate);
            game.PlayerHasFlag += new cGame.PlayerUpdateLisner(game_PlayerHasFlag);
            game.PlayerIsDead += new cGame.PlayerUpdateLisner(game_PlayerIsDead);
            game.PlayerReturnedFlag += new cGame.PlayerUpdateLisner(game_PlayerReturnedFlag);
            game.PlayerInSpawnPoint += new cGame.PlayerUpdateLisner(game_PlayerInSpawnPoint);
            game.SetTeam(new cTeam(Properties.Settings.Default.MyTeamColor, Properties.Settings.Default.InitialFlagLocation));

            if (Properties.Settings.Default.MyTeamColor == Color.Red)
            {
                game.MyTeam.AddPlayer(1, "Pizza", "c:\\pic.jpeg", Properties.Settings.Default.Port);
                game.MyTeam.AddPlayer(2, "Pasta", "c:\\pic.jpeg", Properties.Settings.Default.Port);
                game.MyTeam.AddPlayer(3, "Canaloni", "c:\\pic.jpeg", Properties.Settings.Default.Port);
            }
            else
            {
                game.MyTeam.AddPlayer(4, "Sushi", "c:\\pic.jpeg", Properties.Settings.Default.Port);
                game.MyTeam.AddPlayer(5, "Yakitori", "c:\\pic.jpeg", Properties.Settings.Default.Port);
                game.MyTeam.AddPlayer(6, "Ekonomiyaki", "c:\\pic.jpeg", Properties.Settings.Default.Port);
            }

            game.UpdateEnemyInfo(Properties.Settings.Default.Port,
                                 Properties.Settings.Default.IP);

           // game.LocationUpdate += new cGame.LocationUpdateLisner(game_LocationUpdate);



            // game.SetTeam(new cTeam(BL.get));

            /*
              for (int player = 0; player < 3; player++)
            {
               // this.lstTeam1List.DataSource = game.TeamsList[0].PlayersList;
               // this.lstTeam2List.DataSource = game.TeamsList[1].PlayersList;
            }
             */

            this.lblcurrRound.Text = "Current round: " + game.RoundsNumber.ToString();
            this.lblPlayer1Name.Text = game.MyTeam.PlayersList[0].Name;
            this.lblPlayer2Name.Text = game.MyTeam.PlayersList[1].Name;
            this.lblPlayer3Name.Text = game.MyTeam.PlayersList[2].Name;

            this.lblPlayer4Name.Text = game.EnemyTeam.PlayersList[0].Name;
            this.lblPlayer5Name.Text = game.EnemyTeam.PlayersList[1].Name;
            this.lblPlayer6Name.Text = game.EnemyTeam.PlayersList[2].Name;

            this.tmrUpdate.Enabled = true;


 //           this.game.startGame(Settings.Default.Port);
        }

        private void game_PlayerReturnedFlag(int playerID)
        {
            MessageBox.Show(string.Format("player : {0} is reyurned falg",
                              playerID));
        }

        private void game_PlayerInSpawnPoint(int playerID)
        {
            MessageBox.Show(string.Format("player : {0} is in spawn point",
                              playerID));
        }

        private void game_PlayerIsDead(int playerID)
        {
            MessageBox.Show(string.Format("player : {0} is dead",
                                          playerID));
        }

        private void game_PlayerHasFlag(int playerID)
        {
            MessageBox.Show(string.Format("Player : {0} has flag",
                                          playerID));
        }

        void game_LocationUpdate(int nPlayerId, Point pnLocationPlayer)
        {
            lstEvents.Items.Add(
                string.Format("Id: {0}, X: {1}, y: {2}", nPlayerId, pnLocationPlayer.X.ToString(),
                              pnLocationPlayer.Y.ToString()));
        }
    }
}
